//
//  JPNavigationBar.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/4/19.
//

#import "JPNavigationBar.h"

static CGFloat __kJPNavigationBarItemLeftPadding = 15.0;
static CGFloat __kJPNavigationBarItemRightPadding = 15.0;


@interface JPNavigationBar(){
    NSMutableDictionary<NSNumber *,UIView *> *_positoinViews;
}
@property (nonatomic,strong) UIView *bottomLine;

@end

@implementation JPNavigationBar

- (instancetype)init{
    return [self initWithFrame:(CGRectZero)];
}

- (instancetype)initWithFrame:(CGRect)frame{
    CGFloat currentAvailableWidth = [DeviceUtils currentAvailableWidth];
    CGRect rect = (CGRect){frame.origin.x,frame.origin.y,currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT};
    self = [super initWithFrame:rect];
    if (self){
        _positoinViews = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    CGFloat currentAvailableWidth = [DeviceUtils currentAvailableWidth];
    CGRect rect = (CGRect){frame.origin.x,frame.origin.y,currentAvailableWidth,kYM_NAVIGATIONBAR_HEIGHT};
    [super setFrame:rect];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *view = [super hitTest:point withEvent:event];
    if ([view isEqual:self] &&
        point.x <= self.leftButton.left &&
        point.y >= self.leftButton.top &&
        point.y <= self.leftButton.bottom) {
        return self.leftButton;
    }
    if ([view isEqual:self] &&
        point.x >= self.rightButton.right &&
        point.y >= self.rightButton.top &&
        point.y <= self.rightButton.bottom) {
        return self.rightButton;
    }
    return view;
}

- (void)setItemView:(UIView *)itemView inPosition:(JPBarButtonItemPosition)position{
    if (_positoinViews[@(position)] && [_positoinViews[@(position)] isDescendantOfView:self]){
        [_positoinViews[@(position)] removeFromSuperview];
    }
    _positoinViews[@(position)] = itemView;
    if (itemView == nil) return;
    
    [self addSubview:itemView];
    switch (position) {
        case JPBarButtonItemPosition_Left:{
            if ([itemView isKindOfClass:[UIButton class]]){
                _leftButton = (UIButton *)itemView;
            }
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.mas_bottom).mas_offset(-22);
                make.leading.mas_equalTo(__kJPNavigationBarItemLeftPadding);
            }];
            break;
        }
            
        case JPBarButtonItemPosition_Right:  {
            if ([itemView isKindOfClass:[UIButton class]]){
                _rightButton = (UIButton *)itemView;
            }
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.mas_bottom).mas_offset(-22);
                make.trailing.equalTo(self).mas_offset(__kJPNavigationBarItemRightPadding * -1);
            }];
            break;
        }
            
        case JPBarButtonItemPosition_Middle: {
            
            _titleView = itemView;

            if ([itemView isKindOfClass:[UILabel class]]
                ||[itemView isKindOfClass:[UIButton class]]){
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self);
                    make.centerY.equalTo(self.mas_bottom).mas_offset(-22);
                }];
            }
            break;
        }

    }
}

- (void)setTitleView:(UIView *)titleView{
    [self setItemView:titleView inPosition:JPBarButtonItemPosition_Middle];
}

- (void)setLeftButton:(UIButton *)leftButton{
    _leftButton = leftButton;

    [self setItemView:leftButton inPosition:JPBarButtonItemPosition_Left];

}

- (void)setRightButton:(UIButton *)rightButton{
    _rightButton = rightButton;

    [self setItemView:rightButton inPosition:JPBarButtonItemPosition_Right];
}


- (void)setTitle:(NSString *)title{

    
}

- (void)setHidden:(BOOL)hidden{
    [super setHidden:hidden];
}


@end


@implementation JPNavigationBar (EasySet)

- (void)setTitle:(NSString *)title
{
    if (!title.length){
        [self setItemView:nil inPosition:JPBarButtonItemPosition_Middle];
    }else{
        if ([_positoinViews[@(JPBarButtonItemPosition_Middle)] isKindOfClass:[UILabel class]]){
            UILabel *label = (UILabel *)_positoinViews[@(JPBarButtonItemPosition_Middle)];
            [label setText:title];
            return;
        }
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = KFontBlackColor;
        titleLabel.font = JPUIFontPingFangSCMedium(17);
        titleLabel.text = title;
        [self setItemView:titleLabel inPosition:JPBarButtonItemPosition_Middle];
    }
}

- (void)setTitle:(NSString *)title inPosition:(JPBarButtonItemPosition)position
{
    if (position == JPBarButtonItemPosition_Middle){
        self.title = title;
    }
    [self __setupButton:^{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = JPUIFontPingFangSCMedium(16);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateSelected];
        return button;
    }() inPosition:position];
}
- (void)setIcon:(UIImage *)icon inPosition:(JPBarButtonItemPosition)position
{
    [self __setupButton:^{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:icon forState:UIControlStateNormal];
        [button setImage:icon forState:UIControlStateSelected];
        return button;
    }() inPosition:position];
}


- (void)__setupButton:(UIButton *)button inPosition:(JPBarButtonItemPosition)position
{
    [button sizeToFit];
    
    switch (position) {
        case JPBarButtonItemPosition_Left:{
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            CGFloat h_move = 0;
            CGFloat v_move = 0;
            
            if (button.width < 50){
                h_move = 50-button.width;
            }
            if (button.height < 40){
                v_move = 40-button.height;
            }
            if (v_move != 0 || h_move != 0){
                button.contentEdgeInsets = (UIEdgeInsets){v_move/2.0,0,v_move/2.0,h_move};
            }
            
            self.leftButton = button;
            break;
        }
            
        case JPBarButtonItemPosition_Right:  {
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            CGFloat h_move = 0;
            CGFloat v_move = 0;
            
            if (button.width < 50){
                h_move = 50-button.width;
            }
            if (button.height < 40){
                v_move = 40-button.height;
            }
            if (v_move != 0 || h_move != 0){
                button.contentEdgeInsets = (UIEdgeInsets){v_move/2.0,h_move,v_move/2.0,0};
            }
            self.rightButton = button;
            break;
        }
            
        case JPBarButtonItemPosition_Middle: {
            self.titleView = button;
            break;
        }
            
    }
}


- (void)setLeftItemWithTitle:(NSString *)title
{
    [self setTitle:title inPosition:JPBarButtonItemPosition_Left];
}
- (void)setRightItemWithTitle:(NSString *)title
{
    [self setTitle:title inPosition:JPBarButtonItemPosition_Right];
}
- (void)setLeftItemWithIcon:(UIImage *)icon
{
    [self setIcon:icon inPosition:JPBarButtonItemPosition_Left];
}
- (void)setRightItemWithIcon:(UIImage *)icon
{
    [self setIcon:icon inPosition:JPBarButtonItemPosition_Right];
}

@end
